import modelExtend from 'dva-model-extend'
import * as duplicateFriendService from './services/duplicateFriend'
import {pageModel} from 'utils/model'

const {query} = duplicateFriendService

export default modelExtend(pageModel, {
  namespace: 'duplicateFriend',
  state: {
    modalVisible: false,
    detailList: [],
  },
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen((location) => {
        if (location.pathname === '/duplicateFriend') {
          const payload = location.query || {current: 1, size: 10}
          dispatch({
            type: 'query',
            payload,
          })
        }
      })
    },
  },
  effects: {
    * query ({payload = {}}, {call, put}) {
      const response = yield call(query, payload)
      const {data} = response
      if (response && data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.records,
            pagination: {
              current: Number(payload.current) || 1,
              pageSize: Number(payload.size) || 10,
              total: data.total,
            },
          },
        })
      }
    },

    * duplicateFriendDetail ({payload = {}}, {call, put}) {
      const response = yield call(duplicateFriendService.detailList, payload)
      const {data} = response
      if (response && data) {
        yield put({
          type: 'updateState',
          payload: {
            detailList: data,
          },
        })
      }
    },
  },
  reducers: {

    showModal (state, {payload}) {
      return {...state, ...payload, modalVisible: true}
    },

    hideModal (state) {
      return {...state, modalVisible: false}
    },
  },
})
